<template>
  <div class="wrapper">
    <div class="login-content">
      <div style="padding-left:40px">双语儿童后台管理系统</div>
      <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="80px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="pass">
          <el-input type="text" v-model="ruleForm2.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input type="password" v-model="ruleForm2.pwd" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item class="button">
          <el-button type="danger" @click="submitForm('ruleForm2')">提交</el-button>
          <el-button @click="resetForm('ruleForm2')" type="success">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
  import {login} from '@/api/login/login'
  import axois from 'axios'

  export default {
    name: 'Login',
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }
        {
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      return {
        ruleForm2: {
          pass: '',
          checkPass: '',
          pwd: ''
        },
        rules2: {
          pass: [
            {validator: validatePass, trigger: 'blur'}
          ],
          pwd: [
            {validator: validatePass2, trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm2);
            let params = {};
            params.username = this.ruleForm2.pass;
            params.password = this.ruleForm2.pwd;
            var _this = this;
            login(params).then(function (res) {
              console.log(res);
              if (res.data.code==0) {
                localStorage.ms_username = res.data.data.actor;
                _this.$store.commit({
                  type: 'saveuserinfo',
                  actor: res.data.data.actor,
                  name: res.data.data.name,
                  id: res.data.data.id,
                  status: res.data.data.status
                });
                localStorage.userinfo = JSON.stringify(res.data.data);
                _this.$router.push('/');
              } else if (res.data.message) {
                _this.$message.error(res.data.message);
              } else {
                _this.$message.error('您的账号密码不正确');
              }
            })
            // axois.get('/account/login',{
            //   params:params
            // })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style lang="less" scoped>
  @import "~styles/z.less";
  @import "~styles/css3.less";
  @import "~styles/variables.less";

  .wrapper {
    .w100;
    padding-bottom: 5%;
    background-size: cover;
    .abs;

    .login-content {
      width: 400px;
      height: 400px;
      margin: 100px auto;
      background: #409EFF;
      border-radius: 20px;
      .flex;
      align-items: center;
      flex-direction: column;
      padding: 50px 0px;
      /deep/ .el-form-item__label {
        color: white;
        font-size: 18px;
        margin-top: 30px;
        margin-left: -30px;
      }
      /deep/ .el-input__inner {
        margin-left: -10px;
        margin-top: 30px;
      }
      div {
        color: white;
        font-size: 20px;
        margin-bottom: 30px;
      }
      .button {
        margin-top: 60px;
        margin-left: -70px;
        text-align: center;
      }
    }
  }
</style>
